<?php
    
    class Uploader
    {
        const UPLOADS_ROOT             = 'upload/';
        const STATUS_SUCCESS           = 200;
        const STATUS_ALREADY_EXISTS    = 409;
        const STATUS_INCOMPLETE_UPLOAD = 412;
        const STATUS_MAX_SIZE_EXCEEDED = 413;
        const STATUS_SERVER_ERROR      = 500;
        const STATUS_NO_TMP_DIR        = 505;
        const STATUS_ABORTED_BY_EXT    = 506;
        const STATUS_ACCESS_DENIED     = 507;
        const STATUS_INVALID_PATH      = 510;
        
        /**
         * Upload files using $_FILES
         *
         * @param string $dir Path to directory relative to upload dir
         * @param string $folder
         * @param integer $sizeLimit
         * @param string $key
         */
        public static function Upload( $dir, $sizeLimit, $key = 'Filedata' )
        {
            if ( !self::IsValidDir( $dir ) )
            {
                return self::STATUS_INVALID_PATH;
            }

            $size = $_FILES[$key]['size'];
            if ( $size > $sizeLimit )
            {
                return self::STATUS_MAX_SIZE_EXCEEDED;
            }

            $result = self::STATUS_SERVER_ERROR;
            try
            {
                $uploadPath = self::GetUploadsAbsolutePath() . $dir . '/' . $_FILES[$key]['name'];
                if ( is_file($uploadPath) )
                {
                    $result = self::STATUS_ALREADY_EXISTS;
                }
                else if ( !move_uploaded_file( $_FILES[$key]['tmp_name'], $uploadPath) )
                {
                    $result = $this->GetUploadResult($_FILES[$key]['error']);
                }
                else
                {
                    $result = self::STATUS_SUCCESS;
                }
            }
            catch (Exception $e)
            {
            }

            return $result;
        }
        
        /**
         * Returns upload dir or FALSE
         *
         * @param string $dir
         */
        
        public function IsValidDir( $dir )
        {
            return is_dir( self::GetUploadsAbsolutePath() . $dir );
        }

        public static function GetUploadsAbsolutePath()
        {
            return ROOT_PATH . self::UPLOADS_ROOT;
        }

        private function GetUploadResult( $error )
        {
            switch ($error)
            {
                case UPLOAD_ERR_INI_SIZE:
                case UPLOAD_ERR_FORM_SIZE:
                    $result = self::STATUS_MAX_SIZE_EXCEEDED;
                    break;

                case UPLOAD_ERR_CANT_WRITE:
                    $result = self::STATUS_ACCESS_DENIED;
                    break;

                case UPLOAD_ERR_NO_TMP_DIR:
                    $result = self::STATUS_NO_TMP_DIR;
                    break;

                case UPLOAD_ERR_EXTENSION:
                    $result = self::STATUS_ABORTED_BY_EXT;
                    break;

                case UPLOAD_ERR_PARTIAL:
                case UPLOAD_ERR_NO_FILE:
                    $result = self::STATUS_INCOMPLETE_UPLOAD;
                    break;

                default:
                    $result = self::STATUS_SERVER_ERROR;
            }

            return $result;
        }
    }
?>